package pruebas;

import junit.framework.TestCase;
import negModel.*;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;



public class PistaTest extends TestCase {

	private Pista p;
	Element e;
	
	
	protected void setUp() throws Exception{
		super.setUp();
		p= new Pista(1.0,"Asfalto",40.0,0.1,"Hielo");
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	    DocumentBuilder loader = factory.newDocumentBuilder();
	    Document doc = loader.newDocument();
		
		e= doc.createElement("pista");
				
		e.setAttribute("longitud", ""+ p.getLongitud());
				
		e.appendChild((p.getSuperficie()).toXml(doc));
		e.appendChild((p.getClima()).toXml(doc));
			
	}
	
	public void testCoeficiente() {
		assertEquals(1.1,p.getCoeficiente(),0.0);
	}
	
	public void testLongitud() {
		assertEquals(40.0,p.getLongitud(),0.0);
	}
	
	public void testCambiarLongitud() {
		p.setLongitud(30.0);
		assertEquals(30.0,p.getLongitud(),0.0);
	}
	
	public void testClima () {
		
		Pista p2=new Pista(e);
		
		assertEquals(p2.getLongitud(),Double.parseDouble(e.getAttribute("longitud")),0.0);
		assertNotNull(p2.getClima());
		assertNotNull(p2.getSuperficie());
	}
	
	public void testToXml () throws Exception {
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	    DocumentBuilder loader = factory.newDocumentBuilder();
	    Document doc = loader.newDocument();
		
		e=p.toXml(doc);
		
		assertEquals(Double.parseDouble(e.getAttribute("longitud")),p.getLongitud(),0.0);
		
		NodeList nodo=e.getElementsByTagName("superficie");
		Superficie s= new Superficie((Element)nodo.item(0));
		
		nodo=e.getElementsByTagName("clima");
		Clima c= new Clima((Element)nodo.item(0));
		
		assertNotNull(s);
		assertNotNull(c);
	}

}

